package com.jby.autotest.service.tender.impl;

import com.jby.autotest.common.Constants;
import com.jby.autotest.dao.TenderMapper;
import com.jby.autotest.model.Tender;
import com.jby.autotest.service.tender.PublishFirstTenderService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PublishFirstTenderServiceImpl implements PublishFirstTenderService {

    private static final Logger logger = LogManager.getLogger(PublishFirstTenderServiceImpl.class);

    @Autowired
    private TenderMapper tenderMapper;

    @Override
    public List<Integer> publishFirstTender(Tender tender) throws Exception {

        int tenderType;
        boolean isTrue;
        int tenderId;
        List<Integer> tenderIdList = new ArrayList<>();

        tenderIdList.add(111456);
        tenderIdList.add(111457);
//        tenderIdList.add(111458);
//        tenderIdList.add(111459);
//        tenderIdList.add(111460);
        tender.setBorrowerId(871222);
        tender.setBorrowerName("小江");
        isTrue = true;



        for (int i = 0; i < tenderIdList.size(); i++) {

            tenderId = tenderIdList.get(i);
            tender.setTenderId(tenderId);
            Date date = new Date();
            tender.setCreateTime(date);
            tender.setUpdateTime(date);
            tenderType = tender.getTenderType();
            tender.setStatus(Constants.loanApply);
            tenderMapper.insertSelective(tender);
            logger.info("发标成功！tenderId：" + tenderId);
            logger.info("publishFirstTender.insertDTenderReviewIdeaRecordWithMengShang:: 插入盟商审核意见是否成功： " + true);

            if (isTrue) {
                isTrue = insertDTenderReviewIdeaRecordWithMengShang(tenderId);
                tender.setStatus(Constants.insertDTenderReviewIdeaRecordWithMengShang);
                tender.setUpdateTime(new Date());
                tenderMapper.updateByTenderIdSelective(tender);
                logger.info("publishFirstTender.insertDTenderReviewIdeaRecordWithMengShang:: 插入盟商审核意见： " + isTrue);
            }

            if (isTrue) {
                isTrue = insertLoanClassify2(tenderId, tender.getBorrowerId(), tender.getTenderPurposeOfBorrowing());
                tender.setStatus(Constants.insertLoanClassify2);
                tender.setUpdateTime(new Date());
                tenderMapper.updateByTenderIdSelective(tender);
                logger.info("publishFirstTender.insertLoanClassify2:: 上传材料是否成功： " + isTrue);
            }

            if (isTrue && (
                    Constants.TENDER_TYPE_CAR_TENDER == tenderType ||
                            Constants.TENDER_TYPE_GUARANTEE_FOR_HOUSE == tenderType ||
                            Constants.TENDER_TYPE_GUARANTEE_FOR_HOUSE_XKF == tenderType ||
                            Constants.TENDER_TYPE_GUARANTEE_FOR_HOUSE_DQ == tenderType)
            ) {
                isTrue = saveAssetDetails(tenderId, tenderType);
                tender.setStatus(Constants.saveAssetDetails);
                tender.setUpdateTime(new Date());
                tenderMapper.updateByTenderIdSelective(tender);
                logger.info("publishFirstTender.saveAssetDetails:: 保存车房贷资产明细信息是否成功： " + isTrue);
            }

            if (isTrue) {
                isTrue = updateBorrowBasicInfoAndIndustry(tenderId);
                tender.setStatus(Constants.updateBorrowBasicInfoAndIndustry);
                tender.setUpdateTime(new Date());
                tenderMapper.updateByTenderIdSelective(tender);
                logger.info("publishFirstTender.updateBorrowBasicInfoAndIndustry:: 完善借款人个人信息（从事行业）是否成功： " + isTrue);
            }

            //借款人添加保险受益人
            saveBeneficiarysWithBorrower(tenderId, tender.getBorrowerId());

            //鑫农贷添加共借人 及保险受益人
            if (Constants.TENDER_TYPE_NEW_NONG_TENDER == tenderType) {
                saveBeneficiarysWithCommonBorrower(tenderId, tender.getBorrowerId());
            }

            if (isTrue) {
                isTrue = updateTenderStatus(tenderId, 11);
                tender.setStatus(Constants.updateTenderStatus);
                tender.setUpdateTime(new Date());
                tenderMapper.updateByTenderIdSelective(tender);
                logger.info("publishFirstTender.updateTenderStatus:: 更新待总部初审状态是否成功： " + isTrue);
            }

            if (isTrue) {
                isTrue = insertDTenderReviewIdeaRecordWithZiGuan(tenderId);
                tender.setStatus(Constants.insertDTenderReviewIdeaRecordWithZiGuan);
                tender.setUpdateTime(new Date());
                tenderMapper.updateByTenderIdSelective(tender);
                logger.info("publishFirstTender.insertDTenderReviewIdeaRecordWithZiGuan:: 插入资管意见是否成功： " + isTrue);
            }

            if (isTrue) {
                isTrue = insertDTenderReviewIdeaRecordWithZongBu(tenderId, Constants.TENDER_REVIEW_IDEA_RECORD_OPERATORTYPE_0);
                tender.setStatus(Constants.insertDTenderReviewIdeaRecordWithZongBu);
                tender.setUpdateTime(new Date());
                tenderMapper.updateByTenderIdSelective(tender);
                logger.info("publishFirstTender.insertDTenderReviewIdeaRecordWithZongBu:: 插入总部意见是否成功： " + isTrue);
            }

            if (isTrue) {
                isTrue = sendToTenderPool(tenderId);
                tender.setStatus(Constants.sendToTenderPool);
                tender.setUpdateTime(new Date());
                tenderMapper.updateByTenderIdSelective(tender);
                logger.info("publishFirstTender.sendToTenderPool:: 推送债权池是否成功：" + isTrue);
            }
        }

        return tenderIdList;
    }

    @Override
    public boolean insertDTenderReviewIdeaRecordWithMengShang(int tenderId)  {

        boolean isTrue = true;

//        if(1 == 1) {
//            throw new NullPointerException("Field error in object 'product' on field 'tenderTitle': rejected value [1]; codes [methodInvocation.product.tenderTitle,methodInvocation.tenderTitle,methodInvocation.java.lang.String,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [product.tenderTitle,tenderTitle]; arguments []; default message [tenderTitle]]; default message [Property 'tenderTitle' threw exception; nested exception is java.lang.NullPointerException]]");
//        }
        return isTrue;

    }

    @Override
    public boolean insertLoanClassify2(int tenderId, int userId, String purposeOfBorrowing) {
        return true;
    }

    @Override
    public boolean saveAssetDetails(int tenderId, int tenderType) {
        return true;
    }

    @Override
    public boolean updateBorrowBasicInfoAndIndustry(int tenderId) {
        return true;
    }

    @Override
    public boolean saveBeneficiarysWithBorrower(int tenderId, int borrowerUid) {
        return true;
    }

    @Override
    public boolean saveBeneficiarysWithCommonBorrower(int tenderId, int borrowerUid) {
        return true;
    }

    @Override
    public boolean updateTenderStatus(int tenderId, int status) {
        return true;
    }

    @Override
    public boolean insertDTenderReviewIdeaRecordWithZiGuan(int tenderId) {
        return true;
    }

    @Override
    public boolean insertDTenderReviewIdeaRecordWithZongBu(int tenderId, int operatorType) {
        return true;
    }

    @Override
    public boolean sendToTenderPool(int tenderId) {
        return true;
    }

    @Override
    public boolean userEnsureOwnTender(List<Integer> tenderIdList, String loanSource) {
        return true;
    }

    @Override
    public boolean tenderFinalCheck(List<Integer> tenderIdList) {
        return true;
    }
}
